package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.entity.*;
import com.qf.enums.EmployerExceptionEnum;
import com.qf.service.*;
import com.qf.utils.R;
import com.qf.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@Controller
@RequestMapping("/position")
@Slf4j
public class PositionController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private PositionClassService positionClassService;
    @GetMapping("/list")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "5")Integer size,
                       String roleName, String deptName, Integer roleStatus, Model model){
    PageInfo<Role> pageInfo = roleService.findAllInfo(pageNum,size,roleName,deptName,roleStatus);
    model.addAttribute("pageInfo",pageInfo);
    model.addAttribute("roleName",roleName);
    model.addAttribute("deptName",deptName);
    model.addAttribute("roleStatus",roleStatus);
    return "/resource/demo3/list";
    }

    @GetMapping("/add-ui")
    public String addUI(Model model){
     List<Dept> deptList= deptService.findAll();
     List<Menu> menuList = menuService.findAllInfo();
     List<PositionClass> positionClasses = positionClassService.findAllInfo();
     model.addAttribute("deptList",deptList);
     model.addAttribute("menuList",menuList);
     model.addAttribute("classify",positionClasses);
     return "/resource/demo3/add";
    }

    @PostMapping("/add")
    @ResponseBody

    public ResultVO add(@Valid Role role, BindingResult bindingResult, int[] mid){
        if(bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【职位管理添加】参数不合法，userPosition={}",role);
            return R.error(EmployerExceptionEnum.USER_PARAM_ERROR.getCode(),msg);
        }
        roleService.add(role,mid);
        return R.ok();
    }

}
